﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;

namespace TiebaSpider
{
    /// <summary>
    /// setup.xaml 的交互逻辑
    /// </summary>
    public partial class setup : Window
    {
        public setup()
        {
            InitializeComponent();

            App application = App.Current as App;

            limitSizeMin.Text = application.SetupController.AppOptions.MinSizeLimitation.ToString();
            limitWidthMin.Text = application.SetupController.AppOptions.MinWidthLimitation.ToString();
            limitHeightMin.Text = application.SetupController.AppOptions.MinHeightLimitation.ToString();
            storeDirectory.Text = application.SetupController.AppOptions.StorageDirectory;
            independentDirectory.IsChecked = application.SetupController.AppOptions.StoreInSubdirectory;
            taskBatch.Text = application.SetupController.AppOptions.TaskBatchSize.ToString();
			pagePartition.Text = application.SetupController.AppOptions.PagePartition.ToString();
			timeout.Text = application.SetupController.AppOptions.CrawlerTimeout.ToString();
			hideExists.IsChecked = application.SetupController.AppOptions.HideExists;
        }

        private void cancelOption_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void confirmOption_Click(object sender, RoutedEventArgs e)
        {
            App application = App.Current as App;
            application.SetupController.AppOptions.MinSizeLimitation = long.Parse(limitSizeMin.Text);
            application.SetupController.AppOptions.MinWidthLimitation = int.Parse(limitWidthMin.Text);
            application.SetupController.AppOptions.MinHeightLimitation = int.Parse(limitHeightMin.Text);
            application.SetupController.AppOptions.StorageDirectory = storeDirectory.Text;
            application.SetupController.AppOptions.StoreInSubdirectory = independentDirectory.IsChecked ?? false;
            application.SetupController.AppOptions.TaskBatchSize = int.Parse(taskBatch.Text);
			application.SetupController.AppOptions.PagePartition = int.Parse(pagePartition.Text);
			application.SetupController.AppOptions.CrawlerTimeout = int.Parse(timeout.Text);
			application.SetupController.AppOptions.HideExists = hideExists.IsChecked ?? false;

            application.SetupController.SaveOptions();
            this.Close();
        }

        private void chooseDirectory_Click(object sender, RoutedEventArgs e)
        {
            App application = App.Current as App;

            FolderBrowserDialog folderSelector = new FolderBrowserDialog();
            System.Windows.Forms.DialogResult folderSelection = folderSelector.ShowDialog();
            if (folderSelection == System.Windows.Forms.DialogResult.OK)
            {
                application.SetupController.AppOptions.StorageDirectory = folderSelector.SelectedPath;
                storeDirectory.Text = application.SetupController.AppOptions.StorageDirectory;
            }
        }
    }
}
